package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.*;
import com.ztjy.purchase.biz.*;
import com.ztjy.repository.biz.RkWarehouseBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/purchase/purchase")
@SessionAttributes({"prolist","pusers","time"})
public class CPurchaseController {
    @Autowired
    private CPurchaseBiz cpbiz;
    @Autowired
    private CProductBiz prbiz;
    @Autowired
    private UsersBiz ubiz;
    @Autowired
    private CTypeBiz tbiz;
    @Autowired
    private CBrandBiz cbbiz;
    @Autowired
    private CFirmBiz cfbiz;
    @Autowired
    private CDetailsBiz dbiz;
    @Autowired
    private RkWarehouseBiz rkbiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 15);
    }

    @MyLog(value = "采购单分页查询操作")
    @RequestMapping("/purchaseAll")
    public String findCPurchaseAll(Model model, String mintime, String maxtime,Double mintotal, Double maxtotal,
                                   @RequestParam(value = "pageno",defaultValue = "1")Integer pageNo,
                                   CPurchase cPurchase, CProduct cProduct){
        Integer pageSize=6;
        Date minTime=null;
        if (mintime!=null && !mintime.equals("")){
            minTime=Date.valueOf(mintime);
        }
        Date maxTime=null;
        if (maxtime!=null && !maxtime.equals("")){
            maxTime=Date.valueOf(maxtime);
        }
        PageInfo<CPurchase> pb = cpbiz.findCPurchaseByCount(pageNo, pageSize, cPurchase, minTime, maxTime, mintotal, maxtotal);
        model.addAttribute("pb",pb);
        model.addAttribute("minTime",minTime);
        model.addAttribute("maxTime",maxTime);
        model.addAttribute("mintotal",mintotal);
        model.addAttribute("maxtotal",maxtotal);
        Users users = new Users();
        users.setJobId(12);
        List<Users> allUsers = ubiz.findAllUsers(users);
        model.addAttribute("pusers",allUsers);
        Date time =new Date(System.currentTimeMillis());
        model.addAttribute("time",time);
        List<CProduct> all = prbiz.findAll(cProduct);
        model.addAttribute("prolist",all);
        model.addAttribute("pur",cPurchase);
        return "forward:/purchase/purchase/purchaseList.html";
    }

    /**
     * 跳转添加页面
     * @param model
     * @return
     */
    @RequestMapping("/goAdd")
    public String goAdd(Model model){
        List<CBrand> allCBrand = cbbiz.findAllCBrand();
        List<CType> allCType = tbiz.findAllCType();
        CFirm cFirm = new CFirm();
        cFirm.setStatus(1);
        List<CFirm> all = cfbiz.findAll(cFirm);
        CProduct cProduct = new CProduct();
        cProduct.setProductStatus(1);
        List<CProduct> allProduct = prbiz.findAll(cProduct);
        model.addAttribute("CBrand",allCBrand);
        System.out.println(allCBrand);
        model.addAttribute("CType",allCType);
        model.addAttribute("CFirm",all);
        model.addAttribute("cProducts",allProduct);
        return "forward:/purchase/purchase/purchaseAdd.html";
    }

    /**
     * 添加操作
     * @param cProducts
     * @param price
     * @param count
     * @param money
     * @param request
     * @return
     */
    @MyLog(value = "采购单添加操作")
    @RequestMapping("/addPurchase")
    public String addPurchase(Integer cProducts, BigDecimal price, Integer count, BigDecimal money, HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String purchaseId = "CG" + sdf.format(new java.util.Date());
        Users users = (Users) request.getSession().getAttribute("currUser");
        Integer uId=users.getuId();
        CPurchase cPurchase = new CPurchase();
        cPurchase.setPurchaseId(purchaseId);
        cPurchase.setCheckStatus(1);
        cPurchase.setPurchaseTime(new java.util.Date());
        cPurchase.setCreatId(uId);
        cPurchase.setTotalMoney(money.doubleValue());

        CDetails cDetails = new CDetails();
        cDetails.setPurchaseId(purchaseId);
        cDetails.setProductId(cProducts);
        cDetails.setPurchasePrice(price);
        cDetails.setCount(count);
        cDetails.setTotalMoney(money);

        cpbiz.addCPurchase(cPurchase,cDetails);
        return "redirect:/purchase/purchase/purchaseAll";
    }

    /**
     * ajax获取商品单价
     * @param productId
     * @return
     */
    @RequestMapping("/ajaxPrice")
    @ResponseBody
    public BigDecimal ajaxPrice(Integer productId){
        BigDecimal productPrice = prbiz.findByIdProduct(productId).getProductPrice();
        return productPrice;
    }

    /**
     * 删除
     * @param purchaseId
     * @return
     */
    @MyLog(value = "采购单删除操作")
    @RequestMapping("/delPurchase")
    public String delCPurchase(String purchaseId){
        boolean b = dbiz.delCDetails(purchaseId);
        boolean flag = cpbiz.delCPurchase(purchaseId);
        if (flag){
            return "redirect:/purchase/purchase/purchaseAll";
        }else {
            return "";
        }
    }

    /**
     * 提交审核
     * @param purchaseId
     * @param checkId
     * @return
     */
    @MyLog(value = "采购单提交审核操作")
    @RequestMapping("/editStatus")
    public String editcheckStatus(String purchaseId, Integer checkId){
        boolean flag = cpbiz.updateStatus(purchaseId, checkId);
        if (flag){
            return "redirect:/purchase/purchase/purchaseAll";
        }else {
            return "";
        }
    }

    /**
     * 查询详情
     * @param purchaseId
     * @param model
     * @return
     */
    @MyLog(value = "采购单查看详情操作")
    @RequestMapping("/viewPurchase")
    public String findPurchaseByPid( String purchaseId, Model model){
        CPurchase byPid = cpbiz.findCPurchaseByPid(purchaseId);
        model.addAttribute("purlist",byPid);
        List<CDetails> cDetails = dbiz.findByIdCDetails(purchaseId);
        model.addAttribute("cDetails",cDetails);
        return "forward:/purchase/purchase/purchaseView.html";
    }

    /**
     * 入库详情
     * @param id
     * @param model
     * @return
     */
    @MyLog(value = "采购单查看入库详情操作")
    @RequestMapping("/stockPurchase/{id}")
    public String findPurchaseAndCKByPid(@PathVariable("id")Integer id,Model model){
        RkWarehouse rkWarehouseById = rkbiz.findRkWarehouseById(id);
        CPurchase purchase = cpbiz.findCPurchaseByPid(rkWarehouseById.getRkIndent());
        List<CDetails> details = dbiz.findByIdCDetails(rkWarehouseById.getRkIndent());
        model.addAttribute("rk",rkWarehouseById);
        model.addAttribute("pur",purchase);
        model.addAttribute("cde",details);
        return "forward:/storage/stock/stockView.html";
    }

    @RequestMapping("/ajaxBrand")
    @ResponseBody
    public List<CType> findCTypeByBrandId(Integer brandId){
        List<CType> cTypeList = tbiz.findCTypesByBId(brandId);
        return cTypeList;
    }

    @RequestMapping("/ajaxBrandAll")
    @ResponseBody
    public List<CBrand> findBrandAll(){
        List<CBrand> allCBrand = cbbiz.findAllCBrand();
        return allCBrand;
    }

    /**
     * 级联查询商品类型
     * @param bid
     * @return
     */
    @RequestMapping("/ajaxType")
    @ResponseBody
    public List<CType> findTypeAjax(Integer bid){
        CType cType = new CType();
        cType.setBrandId(bid);
        cType.setTypeStatus(1);
        List<CType> cTypes = tbiz.findAllCType(cType, 1, 999).getList();
        return cTypes;
    }

    /**
     * 级联查询型号
     * @param tid
     * @return
     */
    @RequestMapping("/ajaxProduct")
    @ResponseBody
    public List<CProduct> findProductAjax(Integer tid){
        CProduct cProduct = new CProduct();
        cProduct.setTypeId(tid);
        cProduct.setProductStatus(1);
        List<CProduct> all = prbiz.findAll(cProduct);
        return all;
    }

    /**
     * 添加级联查询厂商
     * @param pid
     * @return
     */
    @RequestMapping("/ajaxFirm")
    @ResponseBody
    public  List<CProduct> findCFirmAjax(Integer pid){
        CProduct cProduct = new CProduct();
        cProduct.setProductId(pid);
        cProduct.getFirms().setStatus(1);
        List<CProduct> allByPname = prbiz.findAllByPname(cProduct);
        return allByPname;
    }


    /**
     * 修改级联查询厂商
     * @param pid
     * @return
     */
    @RequestMapping("/ajaxFirm2")
    @ResponseBody
    public  List<CProduct> findCFirmAjax2(Integer pid){
        CProduct cProduct = new CProduct();
        cProduct.setProductId(pid);
        List<CProduct> allByPname = prbiz.findAllByPname(cProduct);
        return allByPname;
    }

    /**
     * 跳转到修改页面
     * @param model
     * @param purchaseId
     * @return
     */
    @RequestMapping("/goUpdatePur")
    public String goUpdate(Model model,String purchaseId){
        List<CBrand> allCBrand = cbbiz.findAllCBrand();
        List<CType> allCType = tbiz.findAllCType();
        List<CFirm> all = cfbiz.findAll(null);
        CProduct cProduct = new CProduct();
        cProduct.setProductStatus(1);
        List<CProduct> allProduct = prbiz.findAll(cProduct);
        model.addAttribute("CBrand",allCBrand);
        System.out.println(allCBrand);
        model.addAttribute("CType",allCType);
        model.addAttribute("CFirm",all);
        model.addAttribute("productId",allProduct);
        CPurchase purchase = cpbiz.findCPurchaseByPid(purchaseId);
        model.addAttribute("purchase",purchase);
        List<CDetails> cDetails = dbiz.findByIdCDetails(purchaseId);
        model.addAttribute("cDetails",cDetails);
        return "forward:/purchase/purchase/purchaseUpdate.html";
    }


    @MyLog(value = "采购单修改操作")
    @RequestMapping("/doUpdate")
    public String doUpdate(CDetails cDetails){
        dbiz.updateCDetails(cDetails);
        CPurchase cPurchase = new CPurchase();
        cPurchase.setPurchaseId(cDetails.getPurchaseId());
        cPurchase.setTotalMoney(cDetails.getTotalMoney().doubleValue());
        cpbiz.updateCPurchase(cPurchase);
        return "redirect:/purchase/purchase/purchaseAll";
    }
}
